﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Spring.Web.Mvc;
using System.Text;
using MySSH.Common.Log;
using Spring.Context.Support;
using Spring.Context;

namespace MySSH.Web
{
    public class SpringMvcApplication : Spring.Web.Mvc.SpringMvcApplication
    {
        protected override IDependencyResolver BuildDependencyResolver()
        {
            //string file = new System.IO.FileInfo(Server.MapPath("~") + "\\Config\\SpringNet.config").FullName;
            //return new SpringMvcDependencyResolver(new XmlApplicationContext(file));
            string file = @"file://" + new System.IO.FileInfo(Server.MapPath("~") + "\\Config\\SpringNet.config").FullName;
            IApplicationContext context = new XmlApplicationContext(file);
            if (!ContextRegistry.IsContextRegistered(
                context.Name))
            {

                ContextRegistry.RegisterContext(context);
            }
            return new SpringMvcDependencyResolver(ContextRegistry.GetContext(context.Name));

        }
    }

    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : Spring.Web.Mvc.SpringMvcApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Admin", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            //NHibernate 性能检测程序
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize(); 

            LogHelper.Info(new LogMsg { LogDesc = "程序启动！", ActionType = LogActionType.AppStart });
        }

        protected void Application_End(object sender, EventArgs e)
        {
            //在应用程序关闭时运行的代码
            LogHelper.Info(new LogMsg { LogDesc = "程序结束！", ActionType = LogActionType.AppEnd });
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            //在出现未处理的错误时运行的代码
            if (Server.GetLastError() == null)
                return;

            Exception ex = Server.GetLastError().GetBaseException();
            StringBuilder sb = new StringBuilder("未处理的异常");
            sb.AppendFormat("\r\n\tSOURCE: {0} URL：{1}", ex.Source, Request.Url.OriginalString);
            sb.AppendFormat("\r\n\tFORM: {0}", Server.UrlDecode(Request.Form.ToString()));
            sb.AppendFormat("\r\n\tQUERYSTRING: {0}", Server.UrlDecode(Request.QueryString.ToString()));
            LogHelper.Error(new LogMsg { Operand = ex.Source, LogDesc = sb.ToString() }, ex);
            //Server.ClearError();
        }
    }
}